import { Geometry } from "three/examples/jsm/deprecated/Geometry";
import {
  UVMapping,
  CubeReflectionMapping,
  CubeRefractionMapping,
  EquirectangularReflectionMapping,
  EquirectangularRefractionMapping,
  // SphericalReflectionMapping,
  CubeUVReflectionMapping,
  // CubeUVRefractionMapping,
  // LegacyJSONLoader,
  RepeatWrapping,
  ClampToEdgeWrapping,
  MirroredRepeatWrapping,
  NearestFilter,
  NearestMipmapNearestFilter,
  NearestMipmapLinearFilter,
  LinearFilter,
  LinearMipmapNearestFilter,
  LinearMipmapLinearFilter,
  
} from "three";
import * as THREE from "three"
import { BufferAttribute } from "three";
import { Color } from "three";
import { Object3D } from "three";
import { Group } from "three";
import { InstancedMesh } from "three";
import { Sprite } from "three";
import { Points } from "three";
import { Line } from "three";
import { LineLoop } from "three";
import { LineSegments } from "three";
import { LOD } from "three";
import { Mesh } from "three";
import { SkinnedMesh } from "three";
import { Shape } from "three";
import { Fog } from "three";
import { FogExp2 } from "three";
import { HemisphereLight } from "three";
import { SpotLight } from "three";
import { PointLight } from "three";
import { DirectionalLight } from "three";
import { AmbientLight } from "three";
import { RectAreaLight } from "three";
import { OrthographicCamera } from "three";
import { PerspectiveCamera } from "three";
import { Scene } from "three";
import { CubeTexture } from "three";
import { Texture } from "three";
import { ImageLoader } from "three";
import { LoadingManager } from "three";
import { AnimationClip } from "three";
import { MaterialLoader } from "three";
import { LoaderUtils } from "three";
import { BufferGeometryLoader } from "three";
import { Loader } from "three";
import { FileLoader } from "three";
import * as Geometries from "three";
import * as Curves from "three";
import axios from 'axios';
/**
 * @author mrdoob / http://mrdoob.com/
 */

class MyObjectLoader {
  resourcePath: string;
  last: string;
  constructor(path: string, last: string,onLoad:any) {
    this.resourcePath = path;
    this.last = last;
	this.initViewData(  this.resourcePath,onLoad)
  }
  initViewData=async(modelPath,onLoad)=> {
       
	// let url = '/resource/vtk/res_9.vtu';
	// const modelPath="/three/json/teapot-claraio.json"
	axios.get(modelPath,{ responseType: 'json'}).then(async (result:any)=>{
	  // const result: DispersedResult =res ;
	  debugger
	  await this.parse(result.data,onLoad);
	})
  
  }
  parse(json,onLoad){
	var shapes = this.parseShape( json.shapes );
	var geometries = this.parseGeometries( json.geometries, shapes );
	var images = this.parseImages( json.images, function () {

		if ( onLoad !== undefined ) onLoad( object );

	} );
	var textures = this.parseTextures( json.textures, images );
	var materials = this.parseMaterials( json.materials, textures );

	var object = this.parseObject( json.object, geometries, materials );
  }
  parseMaterials(json, textures){

  }
  parseTextures( json, images){

  }
  parseImages(json, onLoad){

  }
  parseShape ( json ) {

  }
  parseGeometries( json, shapes){
	var geometries = {};
	if ( json !== undefined ) {
		var bufferGeometryLoader = new BufferGeometryLoader();
		for ( var i = 0, l = json.length; i < l; i ++ ) {
			var geometry;
			var data = json[ i ];
			switch ( data.type ) {

				case 'PlaneGeometry':
				case 'PlaneBufferGeometry':

					geometry = new Geometries[ data.type ](
						data.width,
						data.height,
						data.widthSegments,
						data.heightSegments
					);

					break;

				case 'BoxGeometry':
				case 'BoxBufferGeometry':
				case 'CubeGeometry': // backwards compatible

					geometry = new Geometries[ data.type ](
						data.width,
						data.height,
						data.depth,
						data.widthSegments,
						data.heightSegments,
						data.depthSegments
					);

					break;

				case 'CircleGeometry':
				case 'CircleBufferGeometry':

					geometry = new Geometries[ data.type ](
						data.radius,
						data.segments,
						data.thetaStart,
						data.thetaLength
					);

					break;

				case 'CylinderGeometry':
				case 'CylinderBufferGeometry':

					geometry = new Geometries[ data.type ](
						data.radiusTop,
						data.radiusBottom,
						data.height,
						data.radialSegments,
						data.heightSegments,
						data.openEnded,
						data.thetaStart,
						data.thetaLength
					);

					break;

				case 'ConeGeometry':
				case 'ConeBufferGeometry':

					geometry = new Geometries[ data.type ](
						data.radius,
						data.height,
						data.radialSegments,
						data.heightSegments,
						data.openEnded,
						data.thetaStart,
						data.thetaLength
					);

					break;

				case 'SphereGeometry':
				case 'SphereBufferGeometry':

					geometry = new Geometries[ data.type ](
						data.radius,
						data.widthSegments,
						data.heightSegments,
						data.phiStart,
						data.phiLength,
						data.thetaStart,
						data.thetaLength
					);

					break;

				case 'DodecahedronGeometry':
				case 'DodecahedronBufferGeometry':
				case 'IcosahedronGeometry':
				case 'IcosahedronBufferGeometry':
				case 'OctahedronGeometry':
				case 'OctahedronBufferGeometry':
				case 'TetrahedronGeometry':
				case 'TetrahedronBufferGeometry':

					geometry = new Geometries[ data.type ](
						data.radius,
						data.detail
					);

					break;

				case 'RingGeometry':
				case 'RingBufferGeometry':

					geometry = new Geometries[ data.type ](
						data.innerRadius,
						data.outerRadius,
						data.thetaSegments,
						data.phiSegments,
						data.thetaStart,
						data.thetaLength
					);

					break;

				case 'TorusGeometry':
				case 'TorusBufferGeometry':

					geometry = new Geometries[ data.type ](
						data.radius,
						data.tube,
						data.radialSegments,
						data.tubularSegments,
						data.arc
					);

					break;

				case 'TorusKnotGeometry':
				case 'TorusKnotBufferGeometry':

					geometry = new Geometries[ data.type ](
						data.radius,
						data.tube,
						data.tubularSegments,
						data.radialSegments,
						data.p,
						data.q
					);

					break;

				case 'TubeGeometry':
				case 'TubeBufferGeometry':

					// This only works for built-in curves (e.g. CatmullRomCurve3).
					// User defined curves or instances of CurvePath will not be deserialized.
					geometry = new Geometries[ data.type ](
						new Curves[ data.path.type ]().fromJSON( data.path ),
						data.tubularSegments,
						data.radius,
						data.radialSegments,
						data.closed
					);

					break;

				case 'LatheGeometry':
				case 'LatheBufferGeometry':

					geometry = new Geometries[ data.type ](
						data.points,
						data.segments,
						data.phiStart,
						data.phiLength
					);

					break;

				case 'PolyhedronGeometry':
				case 'PolyhedronBufferGeometry':

					geometry = new Geometries[ data.type ](
						data.vertices,
						data.indices,
						data.radius,
						data.details
					);

					break;

				case 'ShapeGeometry':
				case 'ShapeBufferGeometry':

					var geometryShapes:any[]= [];

					for ( var j = 0, jl = data.shapes.length; j < jl; j ++ ) {

						var shape = shapes[ data.shapes[ j ] ];

						geometryShapes.push( shape );

					}

					geometry = new Geometries[ data.type ](
						geometryShapes,
						data.curveSegments
					);

					break;


				case 'ExtrudeGeometry':
				case 'ExtrudeBufferGeometry':

					var geometryShapes :any[]= [];

					for ( var j = 0, jl = data.shapes.length; j < jl; j ++ ) {

						var shape = shapes[ data.shapes[ j ] ];

						geometryShapes.push( shape );

					}

					var extrudePath = data.options.extrudePath;

					if ( extrudePath !== undefined ) {

						data.options.extrudePath = new Curves[ extrudePath.type ]().fromJSON( extrudePath );

					}

					geometry = new Geometries[ data.type ](
						geometryShapes,
						data.options
					);

					break;

				case 'BufferGeometry':
				case 'InstancedBufferGeometry':

					geometry = bufferGeometryLoader.parse( data );

					break;

				case 'Geometry':

					if ( 'THREE' in window && 'LegacyJSONLoader'   ) {

						var geometryLoader = new   LegacyJSONLoader();
						geometry = geometryLoader.parse( data, this.resourcePath ).geometry;


					} else {

						console.error( 'THREE.ObjectLoader: You have to import LegacyJSONLoader in order load geometry data of type "Geometry".' );

					}

					break;

				default:

					console.warn( 'THREE.ObjectLoader: Unsupported geometry type "' + data.type + '"' );

					continue;

			}
			geometry.uuid = data.uuid;

				if ( data.name !== undefined ) geometry.name = data.name;
				if ( geometry.isBufferGeometry === true && data.userData !== undefined ) geometry.userData = data.userData;

				geometries[ data.uuid ] = geometry;
		}

	}
  }
}

var TEXTURE_MAPPING = {
  UVMapping: UVMapping,
  CubeReflectionMapping: CubeReflectionMapping,
  CubeRefractionMapping: CubeRefractionMapping,
  EquirectangularReflectionMapping: EquirectangularReflectionMapping,
  EquirectangularRefractionMapping: EquirectangularRefractionMapping,
  SphericalReflectionMapping: SphericalReflectionMapping,
  CubeUVReflectionMapping: CubeUVReflectionMapping,
  CubeUVRefractionMapping: CubeUVRefractionMapping,
};

var TEXTURE_WRAPPING = {
  RepeatWrapping: RepeatWrapping,
  ClampToEdgeWrapping: ClampToEdgeWrapping,
  MirroredRepeatWrapping: MirroredRepeatWrapping,
};

var TEXTURE_FILTER = {
  NearestFilter: NearestFilter,
  NearestMipmapNearestFilter: NearestMipmapNearestFilter,
  NearestMipmapLinearFilter: NearestMipmapLinearFilter,
  LinearFilter: LinearFilter,
  LinearMipmapNearestFilter: LinearMipmapNearestFilter,
  LinearMipmapLinearFilter: LinearMipmapLinearFilter,
};

export { MyObjectLoader };
